package itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import itheima.reggie.common.R;
import itheima.reggie.dto.DishDto;
import itheima.reggie.entity.Dish;
import itheima.reggie.entity.DishFlavor;
import itheima.reggie.mapper.DishMapper;
import itheima.reggie.service.DishFlavorService;
import itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Transactional
@Service("dishService")
public class DishServiceImpl extends ServiceImpl<DishMapper,Dish> implements DishService{
    @Autowired
    DishFlavorService dishFlavorService;

    /**
     * 新增菜品，保存口味
     * @param dto
     */
    @Override
    public void saveWithFlavor(DishDto dto) {
        //保存菜品的基本信息到菜品表
        this.save(dto);
        Long dtoId = dto.getId();
        //保存口味
        for (DishFlavor d:dto.getFlavors()){
            d.setDishId(dtoId);
        }
        dishFlavorService.saveBatch(dto.getFlavors());
    }



    /**
     * 根据id查询菜品与口味
     * @param id
     * @return
     */
    @Override
    public DishDto queryDishWithFlavorById(Long id) {
        //查询菜品基本信息
        Dish dish = this.getById(id);
        //查询口味信息
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> list = dishFlavorService.list(lambdaQueryWrapper);
        //封装到dto
        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(list);
        return dishDto;
    }

    /**
     * 修改菜品信息
     * @param dto
     * @return
     */
    @Override
    public int updateDishWithFlavor(DishDto dto) {
        this.updateById(dto);
        Long id=dto.getId();
        //先清理原本菜品口味
       LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper=new LambdaQueryWrapper<>();
       lambdaQueryWrapper.eq(DishFlavor::getDishId,id);
       dishFlavorService.remove(lambdaQueryWrapper);
        //插入新的口味信息
        List<DishFlavor> flavors=dto.getFlavors();
        flavors.stream().map((item)->{
            item.setDishId(dto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
        return 0;
    }

    /**
     * 删除菜品信息与口味
     * @param ids
     */
    @Override
    public void deleteDishWithFlavor(Long[] ids) {
        for (int i=0;i<ids.length;i++){
            Long id=ids[i];
            this.removeById(id);
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DishFlavor::getDishId,id);
            dishFlavorService.remove(lambdaQueryWrapper);
        }

    }
}
